<template>
	<view class="box fu" :style="{height:screenHeight+'px'}">
		<view class="zz">
			<view class="xx">
				<span>收货人</span>
				<input type="text" placeholder="收货人姓名" placeholder-class="liebinu" v-model="data.userName" />
			</view>
			<view class="xx">
				<span>手机号</span>
				<input type="number" maxlength="11" placeholder="收货人手机号" placeholder-class="liebinu"
					v-model="data.userPhone" />
			</view>
			<view class="xx">
				<span>所在省</span>
				<input type="text" placeholder="请输入所在省" placeholder-class="liebinu" v-model="data.province" />
			</view>
			<view class="xx">
				<span>所在市</span>
				<input type="text" placeholder="请输入所在市" placeholder-class="liebinu" v-model="data.city" />
			</view>
			<view class="xx">
				<span>所在区县</span>
				<input type="text" placeholder="请输入所在区县、乡镇" placeholder-class="liebinu" v-model="data.county" />
			</view>
			<view class="xx" style="border: none;">
				<span>详细地址</span>
				<input type="text" placeholder="街道、门牌号" placeholder-class="liebinu" v-model="data.address" />
			</view>
		</view>
		<button class="dibu cc" @click="submit">保存</button>
	</view>
</template>

<script>
	import {
		adddelivery,
		editdelivery
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				screenHeight: "",
				data: {
					userName: '',
					userPhone: '',
					province: '',
					city: '',
					county: '',
					address: ''
				},
				xin: 0
			}
		},
		onLoad(options) {
			this.screenHeight = uni.getSystemInfoSync().windowHeight;
			let abb = JSON.parse(decodeURIComponent(options.data));
			console.log(options);
			if (options.data == '1') {
				this.xin = 1
				uni.setNavigationBarTitle({
					title: '新建收货地址'
				});
			} else {
				this.data = abb
				this.xin = 0
				uni.setNavigationBarTitle({
					title: '编辑收货地址'
				});
			}
		},
		methods: {
			submit() {
				if (this.data.userName == '' || this.data.userPhone == '' || this.data.province == '' || this.data.city ==
					'' || this.data.county ==
					'' || this.data.address == '') {
					uni.showToast({
						title: '仔细核对,有信息未填写',
						duration: 2000,
						mask: false,
						icon: "none"
					})
				}else if(!/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/.test(this.data.userPhone)) {
					uni.showToast({
						icon: 'error',
						title: '手机号格式有误'
					})
				}else{
					if (this.xin == 1) {
						adddelivery(this.data).then(res => {
							console.log(res);
							if (res.code == 200) {
								uni.showToast({
									title: '创建成功',
									duration: 2000,
									mask: false,
									icon: "none"
								})
								uni.navigateBack()
							} else {
								uni.showToast({
									title: res.msg,
									duration: 2000,
									mask: false,
									icon: "none"
								})
							}
						})
					} else {
						editdelivery(this.data).then(res => {
							console.log(res);
							if (res.code == 200) {
								uni.showToast({
									title: '修改成功',
									duration: 2000,
									mask: false,
									icon: "none"
								})
								uni.navigateBack()
							} else {
								uni.showToast({
									title: res.msg,
									duration: 2000,
									mask: false,
									icon: "none"
								})
							}
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	.dibu {
		position: absolute;
		bottom: 50rpx;
	}

	.fu {
		position: relative;
	}

	.liebinu {
		font-size: 26rpx;
	}

	.cc {
		width: 677rpx;
		height: 80rpx;
		background: linear-gradient(84deg, #24180D 0%, #594629 100%);
		border-radius: 40rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		font-size: 28rpx;
		color: #FFFFFF;
		line-height: 33rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.zz {
		width: 650rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 20rpx;



		.xx {
			display: flex;
			align-items: center;
			border-bottom: 1px solid #EEEEEE;
			height: 100rpx;

			span {
				width: 150rpx;
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #0A0A0A;
				line-height: 36rpx;
			}
		}
	}
</style>